package com.ruoyi.app.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.system.domain.SysArea;
import com.ruoyi.system.service.ISysAreaService;
import com.ruoyi.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Api(tags = "省市区列表")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private ISysAreaService service;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private WxMaService miniApp;

    @ApiOperation(value = "查询省市区", notes = "公共接口", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/selectArea", method = RequestMethod.GET)
    public ApiResult<List<SysArea>> selectArea(@RequestParam(defaultValue = "0")Long pid) {
        QueryWrapper<SysArea> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysArea::getPid,pid);
        List<SysArea> list = service.list(wrapper);
        return ApiResult.success(list);
    }


    @RequestMapping(value = "/selectDictById", method = RequestMethod.GET)
    @ApiOperation(value = "查询词典类型", notes = "公共接口", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult<Map<String, List<SysDictData>>> selectDictById(@ApiParam(name="dictTypes",value = "词典类型")
                                     @RequestParam List<String> dictTypes) {
        Map<String, List<SysDictData>> map = sysDictDataService.selectDictDataLists(dictTypes);
        return ApiResult.success(map);
    }

    /**
     * 通用上传请求
     */
    @PostMapping("/common/upload")
    @ApiOperation(value = "上传文件接口", notes = "公共接口", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ApiResult<LocalityFile> uploadFile(MultipartFile file) throws Exception {
        try {
            boolean b = miniApp.getSecCheckService().checkImage(file.getResource().getFile());
            if(!b){
                return ApiResult.failed("上次图片失败，图片含有违法违规内容");
            }

            // 上传文件路径
            String filePath = "/root/img/upload";
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = "https://yifangku.top" + fileName;
            LocalityFile ajax = new LocalityFile();
            ajax.setFileName(fileName);
            ajax.setUrl(url);
            return ApiResult.success(ajax);
        } catch (Exception e) {
            return ApiResult.failed(e.getMessage());
        }
    }

    @Data
    class LocalityFile{
        private String fileName;

        private String url;
    }
}
